package codegenerator.billmenu.WriteXML;

import cn.xiaotool.core.util.XmlUtil;
import org.w3c.dom.*;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WriteEntityXml {
    public static void main(String[] args) {
        String sourcePath = "D:/personal/comprehensive/xp-framework'sExample/src/main/java/codegenerator/billmenu/XMLFile/billMenuGeneratorConfig.xml";
        String destinationPath = "D:/personal/comprehensive/xp-framework'sExample/src/main/java/codegenerator/billmenu/XMLFile/EntityConfig.xml";

        List<Map<String, String>> list = new ArrayList<>();
        NodeList targetNodeList = null;
        getEntityNodeList(targetNodeList,sourcePath);


    }


    public static void writeXMLFile(String path){
        // 读XML文件
        Document document = XmlUtil.readXML(new File(path));
        Element destinationElement = document.getDocumentElement();

        destinationElement.appendChild(document);
//        destinationElement.appendChild();
    }

    public static void getEntityNodeList(NodeList targetNodeList, String path){
        // 读XML文件
        Document document = XmlUtil.readXML(new File(path));
        Element element = document.getDocumentElement();
        parseElement(targetNodeList,element);
    }

    private static void parseElement(NodeList targetNodeList,  Element element) {
        String tagName = element.getTagName();
        // 拿到当前element的子节点集合
        NodeList nodeList = element.getChildNodes();
        Node childNode;
        // 有可能没有子节点,做一个非空判断
        if(nodeList.getLength() != 0){
            // 遍历当前element的所有的子节点
            for (int i = 0; i < nodeList.getLength(); i++) {
                childNode = nodeList.item(i);
                if(childNode.getNodeType() == Node.ELEMENT_NODE){
                    parseElement(targetNodeList,(Element)childNode);
                }
            }
        }
    }


}
